import jspredict from 'jspredict'
import {formatTimestamp} from './dateUtil';

const transitPredict = (tle,loc)=>{
    const qth = [loc.latitude,loc.longitude,loc.altitude]

    var date = new Date();
    // 当前时间戳+3600s（一小时，其他时间通过计算时间戳进行相应加减），重新设置 Date 对象
    date.setTime(new Date().getTime() + 3600000);

    const transit  = jspredict.transitSegment(tle,qth,new Date().getTime(),date.getTime())
    // console.log('transit:',transit)
    if(!transit) return null
    const {start,end,duration} = transit
    // console.log('start:',formatTimestamp(new Date(start)))
    // console.log('end:',formatTimestamp(new Date(end)))


    const {elevation,azimuth} = jspredict.observe(tle,qth)
    let startTime,startTimeZh,endTime = null;
    let restMinutes = 0
    const now = new Date().getTime()
    if(elevation > 0){
        // formatTimestamp(new Date(end))
        if(duration > 0){
            // console.log('duration:',duration)
            const min = parseInt(duration / 1000 / 60 )
            restMinutes =  min < 1 ? 1 : min
        }
        // console.log('end:',formatTimestamp(new Date(end)))
        // console.log('start:',formatTimestamp(new Date(start)))
    }else{
        // formatTimestamp(new Date(start))
        if( (start - now) < 0) return null
        startTime = getDiffStr(now,start)
        startTimeZh = getDiffZhStr(now,start)
    }
    let tleArr = tle.split('\n')
    let name = tleArr[0].trim()
    let id = tleArr[2].substring(2,8).trim()

    let res = {elevation,azimuth,startTime,startTimeZh,endTime,restMinutes,name,id,duration}
    // console.log('res:',res)
    return res

}


function getDiffStr(t1, t2) {
    var timediff = (t2 - t1) / 1000;
    let str = ''
    //天数
    var days = parseInt(timediff / 3600 / 24);
    //小时
    var hours = parseInt((timediff / 3600) % 60);
    //分钟
    var minutes = parseInt((timediff / 60) % 60);
    //秒
    // var seconds = parseInt(timediff % 60);
    if(days === 0 ){
        if(hours > 0 && minutes > 0) return hours + "h" + minutes + "min"
        if(hours > 0 && minutes === 0) return hours + "h"
        if(hours === 0 && minutes > 0) return  minutes + "min"
        if(hours === 0 && minutes === 0) return  "1h"
        // if(hours === 0 && minutes === 0) return parseInt(timediff % 60) + "秒"
    }
    return days + "天" + hours + "时" + minutes + "分"


}

function getDiffZhStr(t1,t2) {
    var timediff = (t2 - t1) / 1000;
    let str = ''
    //天数
    var days = parseInt(timediff / 3600 / 24);
    //小时
    var hours = parseInt((timediff / 3600) % 60);
    //分钟
    var minutes = parseInt((timediff / 60) % 60);
    //秒
    // var seconds = parseInt(timediff % 60);
    if(days === 0 ){
        if(hours > 0 && minutes > 0) return hours + "小时" + minutes + "分"
        if(hours > 0 && minutes === 0) return hours + "小时"
        if(hours === 0 && minutes > 0) return  minutes + "分"
        if(hours === 0 && minutes === 0) return  "1小时"
        // if(hours === 0 && minutes === 0) return parseInt(timediff % 60) + "秒"
    }
    return days + "天" + hours + "时" + minutes + "分"


}


//度转度°分′秒″
function locToDegrees(val) {
    val = val.toString()
    if (typeof (val) == "undefined" || val == "") {
        return "";
    }
    var i = val.indexOf('.');
    var strDu = i < 0 ? val : val.substring(0, i);//获取度
    var strFen = 0;
    var strMiao = 0;
    if (i > 0) {
        var strFen = "0" + val.substring(i);
        strFen = strFen * 60 + "";
        i = strFen.indexOf('.');
        if (i > 0) {
            strMiao = "0" + strFen.substring(i);
            strFen = strFen.substring(0, i);//获取分
            strMiao = strMiao * 60 + "";
            i = strMiao.indexOf('.');
            strMiao = strMiao.substring(0, i + 4);//取到小数点后面三位
            strMiao = parseFloat(strMiao).toFixed(2);//精确小数点后面两位
        }
    }

    return strDu + "°" + strFen + "'" + strMiao + "\"";    // 这里可以修改成你想要的格式例如你可以
    //    改成这样的：return strDu + "°" + strFen + "'" + strMiao + "\"N";

}




export  {
    locToDegrees,
    transitPredict
}
